package Game;

import Ai.FuzzySet;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;

/**
 * @author Emery
 */
public class Launcher extends javax.swing.JFrame {
    public FuzzySet activeRule;
    
    /**
     * Creates new form Launcher
     */
    public Launcher() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        //initiate
        Settings.loadConfig();
        initComponents();
        activeRule = Settings.sets.get(0);
        
        //set loaded settings
        playerName.setText(Settings.playerName);
        showPath.setSelected(Settings.showPath);
        showRay.setSelected(Settings.showRay);
        showSearchSpace.setSelected(Settings.showSearchSpace);
        numberBots.setValue(Settings.numBots);
        numPoints.setValue(activeRule.getX_coord().length);
    }
    
    /**
     * Used to create the list of rules
     * @return a list of rules
     */
    public String[] getList() {
        String[] list = new String[Settings.sets.size()];
        int i = 0;
        
        for(FuzzySet set: Settings.sets) {
            list[i++] = set.getSet() + " " + set.getName();
        }
        
        return list;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        playerName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        showPath = new javax.swing.JRadioButton();
        showRay = new javax.swing.JRadioButton();
        showSearchSpace = new javax.swing.JRadioButton();
        numberBots = new javax.swing.JSpinner();
        ruleList = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        launch = new javax.swing.JButton();
        save = new javax.swing.JButton();
        numPoints = new javax.swing.JSpinner();
        ruleDisplay1 = new Game.RuleDisplay();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        jLabel1.setText("Player Name:");

        playerName.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        playerName.setText("Player 1");

        jLabel5.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        jLabel5.setText("Number of Bots:");

        showPath.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        showPath.setText("Show Path");
        showPath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showPathActionPerformed(evt);
            }
        });

        showRay.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        showRay.setText("Show Ray");
        showRay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showRayActionPerformed(evt);
            }
        });

        showSearchSpace.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        showSearchSpace.setText("Show Search Path");
        showSearchSpace.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showSearchSpaceActionPerformed(evt);
            }
        });

        numberBots.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        numberBots.setModel(new javax.swing.SpinnerNumberModel(0, 0, 10, 1));

        ruleList.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        ruleList.setModel(new DefaultComboBoxModel(getList()));
        ruleList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ruleListActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Bauhaus 93", 1, 48)); // NOI18N
        jLabel2.setText("ShmupBros");

        launch.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        launch.setText("Start Game");
        launch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                launchActionPerformed(evt);
            }
        });

        save.setFont(new java.awt.Font("Bauhaus 93", 0, 14)); // NOI18N
        save.setText("Save Setting");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        numPoints.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10, 1));
        numPoints.setToolTipText("Sets the number of points");
        numPoints.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                numPointsPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(0, 40, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(launch, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(numPoints, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(save, javax.swing.GroupLayout.Alignment.TRAILING)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(launch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(save)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(numPoints, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        numPoints.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                ruleDisplay1.setPoints((int)numPoints.getValue());
            }
        });

        ruleDisplay1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        javax.swing.GroupLayout ruleDisplay1Layout = new javax.swing.GroupLayout(ruleDisplay1);
        ruleDisplay1.setLayout(ruleDisplay1Layout);
        ruleDisplay1Layout.setHorizontalGroup(
            ruleDisplay1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        ruleDisplay1Layout.setVerticalGroup(
            ruleDisplay1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(ruleDisplay1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(showPath)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(numberBots, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(playerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(ruleList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(showRay)
                                .addComponent(showSearchSpace))
                            .addGap(90, 90, 90)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(playerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(numberBots, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(showPath)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(showRay)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(showSearchSpace)
                        .addGap(20, 20, 20)
                        .addComponent(ruleList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ruleDisplay1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void launchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_launchActionPerformed
        Settings.playerName = playerName.getText();
        Settings.showPath = showPath.isSelected();
        Settings.showRay = showRay.isSelected();
        Settings.showSearchSpace = showSearchSpace.isSelected();
        Settings.numBots = (int)numberBots.getValue();        
        
        //starts the game
        try {
            AppGameContainer app = new AppGameContainer(new StateManager());

            app.setDisplayMode(Settings.width, Settings.height, false);
            app.setShowFPS(true);
            app.setAlwaysRender(true); // Display all frames even when not in focus

            dispose();
            app.start();
        } catch (SlickException e) {

        }
    }//GEN-LAST:event_launchActionPerformed

    private void showPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showPathActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_showPathActionPerformed

    private void showRayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showRayActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_showRayActionPerformed

    private void showSearchSpaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showSearchSpaceActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_showSearchSpaceActionPerformed

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        Settings.playerName = playerName.getText();
        Settings.showPath = showPath.isSelected();
        Settings.showRay = showRay.isSelected();
        Settings.showSearchSpace = showSearchSpace.isSelected();
        Settings.numBots = (int)numberBots.getValue(); 
        
        Settings.saveConfig();
    }//GEN-LAST:event_saveActionPerformed

    private void ruleListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ruleListActionPerformed
        int index = ruleList.getSelectedIndex();
        
        activeRule = Settings.sets.get(index);
        ruleDisplay1.changeRule(activeRule);
        numPoints.setValue(activeRule.getX_coord().length);
        ruleDisplay1.revalidate();
        ruleDisplay1.repaint();
    }//GEN-LAST:event_ruleListActionPerformed

    private void numPointsPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_numPointsPropertyChange
    }//GEN-LAST:event_numPointsPropertyChange

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Launcher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Launcher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Launcher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Launcher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Launcher().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton launch;
    private javax.swing.JSpinner numPoints;
    private javax.swing.JSpinner numberBots;
    private javax.swing.JTextField playerName;
    private Game.RuleDisplay ruleDisplay1;
    private javax.swing.JComboBox ruleList;
    private javax.swing.JButton save;
    private javax.swing.JRadioButton showPath;
    private javax.swing.JRadioButton showRay;
    private javax.swing.JRadioButton showSearchSpace;
    // End of variables declaration//GEN-END:variables
}
